<template>
  <div class="coach-list">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>教练列表</span>
          <el-button type="primary" @click="handleAdd">新增教练</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="姓名">
          <el-input v-model="searchForm.name" placeholder="请输入教练姓名" clearable />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="searchForm.phone" placeholder="请输入手机号" clearable />
        </el-form-item>
        <el-form-item label="教练类型">
          <el-select v-model="searchForm.type" placeholder="请选择教练类型" clearable>
            <el-option label="科目二教练" value="subject2" />
            <el-option label="科目三教练" value="subject3" />
            <el-option label="理论教练" value="theory" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="在职" value="active" />
            <el-option label="休假" value="vacation" />
            <el-option label="离职" value="resigned" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column type="expand">
          <template #default="props">
            <el-form label-position="left" inline class="coach-expand">
              <el-form-item label="教龄">
                <span>{{ props.row.experience }}年</span>
              </el-form-item>
              <el-form-item label="准教车型">
                <span>{{ props.row.licenseTypes.join(', ') }}</span>
              </el-form-item>
              <el-form-item label="带教学员数">
                <span>{{ props.row.studentCount }}人</span>
              </el-form-item>
              <el-form-item label="考试通过率">
                <span>{{ props.row.passRate }}%</span>
              </el-form-item>
              <el-form-item label="评分">
                <el-rate
                  v-model="props.row.rating"
                  disabled
                  show-score
                  text-color="#ff9900"
                />
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" width="50" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="phone" label="手机号" width="150" />
        <el-table-column prop="type" label="教练类型" width="120">
          <template #default="scope">
            <el-tag :type="getCoachTypeTag(scope.row.type)">{{ getCoachTypeText(scope.row.type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="workingHours" label="本月工时" width="120" sortable>
          <template #default="scope">
            {{ scope.row.workingHours }}小时
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastActiveTime" label="最近带教时间" width="180" sortable />
        <el-table-column prop="experience" label="教龄" width="80">
          <template #default="scope">
            {{ scope.row.experience }}年
          </template>
        </el-table-column>
        <el-table-column prop="studentCount" label="带教学员数" width="100">
          <template #default="scope">
            {{ scope.row.studentCount }}人
          </template>
        </el-table-column>
        <el-table-column prop="passRate" label="考试通过率" width="100">
          <template #default="scope">
            {{ scope.row.passRate }}%
          </template>
        </el-table-column>
        <el-table-column prop="rating" label="评分" width="100">
          <template #default="scope">
            <el-rate
              v-model="scope.row.rating"
              disabled
              show-score
              text-color="#ff9900"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="280">
          <template #default="scope">
            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="handleSchedule(scope.row)">排班</el-button>
            <el-button link type="primary" @click="handleStudents(scope.row)">学员</el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-drawer
      :title="dialogType === 'add' ? '添加教练' : '编辑教练'"
      v-model="drawerVisible"
      direction="rtl"
      size="50%"
      class="coach-drawer"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="教练类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择教练类型">
            <el-option label="科目二教练" value="subject2" />
            <el-option label="科目三教练" value="subject3" />
            <el-option label="理论教练" value="theory" />
          </el-select>
        </el-form-item>
        <el-form-item label="教龄" prop="experience">
          <el-input-number v-model="form.experience" :min="0" :max="50" />
        </el-form-item>
        <el-form-item label="准教车型" prop="licenseTypes">
          <el-checkbox-group v-model="form.licenseTypes">
            <el-checkbox value="C1">C1</el-checkbox>
          <el-checkbox value="C2">C2</el-checkbox>
          <el-checkbox value="B2">B2</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="active">在职</el-radio>
            <el-radio label="vacation">休假</el-radio>
            <el-radio label="resigned">离职</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="drawerVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-drawer>

    <!-- 学员列表对话框 -->
    <el-dialog
      title="带教学员"
      v-model="studentsDialogVisible"
      width="70%"
    >
      <el-table :data="studentsList" style="width: 100%">
        <el-table-column prop="id" label="学员编号" width="120" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="phone" label="手机号" width="150" />
        <el-table-column prop="subject" label="培训科目" width="120" />
        <el-table-column prop="progress" label="培训进度" width="200">
          <template #default="scope">
            <el-progress :percentage="scope.row.progress" />
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="开始时间" width="180" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'training' ? 'warning' : 'success'">
              {{ scope.row.status === 'training' ? '培训中' : '已结业' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  phone: '',
  type: 'subject2',
  status: 'active'
})

// 表格数据
const loading = ref(false)
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 对话框相关
const drawerVisible = ref(false)
const dialogType = ref('add')
const formRef = ref(null)
const form = reactive({
  name: '',
  phone: '',
  idCard: '',
  type: 'subject2',
  experience: 0,
  licenseTypes: [],
  status: 'active'
})

// 学员列表对话框
const studentsDialogVisible = ref(false)
const studentsList = ref([])

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号', trigger: 'blur' },
    { pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证号', trigger: 'blur' }
  ],
  type: [{ required: true, message: '请选择教练类型', trigger: 'change' }],
  experience: [{ required: true, message: '请输入教龄', trigger: 'change' }],
  licenseTypes: [{ required: true, message: '请选择准教车型', trigger: 'change' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

// 获取教练类型标签
const getCoachTypeTag = (type) => {
  const tagMap = {
    'subject2': 'warning',
    'subject3': 'success',
    'theory': 'info'
  }
  return tagMap[type] || ''
}

// 获取教练类型文本
const getCoachTypeText = (type) => {
  const textMap = {
    'subject2': '科目二教练',
    'subject3': '科目三教练',
    'theory': '理论教练'
  }
  return textMap[type] || type
}

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    'active': 'success',
    'vacation': 'warning',
    'resigned': 'info'
  }
  return typeMap[status] || ''
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    'active': '在职',
    'vacation': '休假',
    'resigned': '离职'
  }
  return textMap[status] || status
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

// 重置搜索
const resetSearch = () => {
  searchForm.name = ''
  searchForm.phone = ''
  searchForm.type = 'subject2'
  searchForm.status = 'active'
  handleSearch()
}

// 获取表格数据
const fetchData = () => {
  loading.value = true
  // 模拟接口请求
  setTimeout(() => {
    tableData.value = [
      {
        name: '李教练',
        phone: '13800138001',
        type: 'subject2',
        workingHours: 120,
        status: 'active',
        lastActiveTime: '2024-03-15 14:30:00',
        experience: 5,
        licenseTypes: ['C1', 'C2'],
        studentCount: 30,
        passRate: 95,
        rating: 4.5
      },
      {
        name: '王教练',
        phone: '13800138002',
        type: 'subject3',
        workingHours: 100,
        status: 'active',
        lastActiveTime: '2024-03-15 16:00:00',
        experience: 8,
        licenseTypes: ['C1', 'B2'],
        studentCount: 25,
        passRate: 92,
        rating: 4.8
      },
      {
        name: '张教练',
        phone: '13800138003',
        type: 'theory',
        workingHours: 80,
        status: 'active',
        lastActiveTime: '2024-03-15 12:00:00',
        experience: 3,
        licenseTypes: ['C1'],
        studentCount: 50,
        passRate: 98,
        rating: 4.6
      },
      {
        name: '刘教练',
        phone: '13800138004',
        type: 'subject2',
        workingHours: 90,
        status: 'vacation',
        lastActiveTime: '2024-03-10 15:30:00',
        experience: 6,
        licenseTypes: ['C1', 'C2'],
        studentCount: 28,
        passRate: 94,
        rating: 4.4
      },
      {
        name: '陈教练',
        phone: '13800138005',
        type: 'subject3',
        workingHours: 110,
        status: 'active',
        lastActiveTime: '2024-03-15 17:30:00',
        experience: 10,
        licenseTypes: ['C1', 'B2', 'C2'],
        studentCount: 35,
        passRate: 96,
        rating: 4.9
      }
    ]
    total.value = 100
    loading.value = false
  }, 500)
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchData()
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

// 新增教练
const handleAdd = () => {
  dialogType.value = 'add'
  drawerVisible.value = true
  Object.assign(form, {
    name: '',
    phone: '',
    idCard: '',
    type: 'subject2',
    experience: 0,
    licenseTypes: [],
    status: 'active'
  })
}

// 编辑教练
const handleEdit = (row) => {
  dialogType.value = 'edit'
  drawerVisible.value = true
  Object.assign(form, row)
}

// 排班
const handleSchedule = (row) => {
  // 实现排班逻辑
}

// 查看学员
const handleStudents = (row) => {
  studentsDialogVisible.value = true
  // 模拟获取学员数据
  studentsList.value = [
      {
        id: '2024001',
        name: '张三',
        phone: '13800138000',
        subject: '科目二',
        progress: 60,
        startDate: '2024-02-01',
        status: 'training'
      },
      {
        id: '2024002',
        name: '李四',
        phone: '13800138001',
        subject: '科目二',
        progress: 85,
        startDate: '2024-01-15',
        status: 'training'
      },
      {
        id: '2024003',
        name: '王五',
        phone: '13800138002',
        subject: '科目二',
        progress: 100,
        startDate: '2024-01-10',
        status: 'completed'
      },
      {
        id: '2024004',
        name: '赵六',
        phone: '13800138003',
        subject: '科目二',
        progress: 40,
        startDate: '2024-02-15',
        status: 'training'
      },
      {
        id: '2024005',
        name: '钱七',
        phone: '13800138004',
        subject: '科目二',
        progress: 75,
        startDate: '2024-01-20',
        status: 'training'
      }
  ]
}

// 删除教练
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确认删除该教练信息吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // 实现删除逻辑
    ElMessage.success('删除成功')
    fetchData()
  }).catch(() => {})
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  try {
    await formRef.value.validate()
    // 实现提交逻辑
    ElMessage.success(dialogType.value === 'add' ? '添加成功' : '修改成功')
    drawerVisible.value = false
    fetchData()
  } catch (error) {
    // 处理验证失败的情况
    console.log('验证失败', error)
  }
}


// 初始化
fetchData()
</script>

<style scoped>
.coach-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.coach-expand {
  padding: 20px;
}

.coach-expand .el-form-item {
  margin-right: 40px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>